/*
 * Selling Partner API for FBA Inbound Eligibilty
 * With the FBA Inbound Eligibility API, you can build applications that let sellers get eligibility previews for items before shipping them to Amazon's fulfillment centers. With this API you can find out if an item is eligible for inbound shipment to Amazon's fulfillment centers in a specific marketplace. You can also find out if an item is eligible for using the manufacturer barcode for FBA inventory tracking. Sellers can use this information to inform their decisions about which items to ship Amazon's fulfillment centers.
 *
 * OpenAPI spec version: v1
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */

package com.amazon.api;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazon.ApiCallback;
import com.amazon.ApiClient;
import com.amazon.ApiException;
import com.amazon.ApiResponse;
import com.amazon.Configuration;
import com.amazon.Pair;
import com.amazon.ProgressRequestBody;
import com.amazon.ProgressResponseBody;
import com.amazon.StringUtil;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentialsProvider;
import com.amazon.SellingPartnerAPIAA.AWSSigV4Signer;
import com.amazon.SellingPartnerAPIAA.LWAAccessTokenCache;
import com.amazon.SellingPartnerAPIAA.LWAAccessTokenCacheImpl;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationSigner;
import com.amazon.model.fbaInbound.GetItemEligibilityPreviewResponse;
import com.google.gson.reflect.TypeToken;

/**
 * 
 * FBA入站资格
 * 借助FBA入站资格API，您可以构建应用程序，让卖家在将商品运送到亚马逊的履行中心之前获得商品的资格预览。
 * 借助此API，您可以确定某件商品是否适合特定市场中的Amazon运营中心的入库货件。
 * 您还可以找出某个项目是否符合使用制造商条形码进行FBA库存跟踪的条件。卖家可以使用此信息来告知他们有关哪些商品要运送到亚马逊运营中心的决定。
 * 
 * getItemEligibilityPreview   此操作将获取您指定的项目的资格预览。您可以指定所需的资格预览类型（INBOUND或COMMINGLING）。对于INBOUND预览，您可以指定要在其中确定项目资格的市场  速率：1个	速率（每秒请求数）/1个
 * INBOUND:入库货件
 * COMMINGLING:使用制造商条形码进行FBA库存跟踪
 * @author guoxin
 *
 */
public class FbaInboundApi {
	private ApiClient apiClient;

	FbaInboundApi() {
		this(Configuration.getDefaultApiClient());
	}

	public FbaInboundApi(ApiClient apiClient) {
		this.apiClient = apiClient;
	}

	public ApiClient getApiClient() {
		return apiClient;
	}

	public void setApiClient(ApiClient apiClient) {
		this.apiClient = apiClient;
	}

	/**
	 * Build call for getItemEligibilityPreview
	 * 
	 * @param asin                    The ASIN of the item for which you want an
	 *                                eligibility preview. (required)
	 * @param program                 The program that you want to check eligibility
	 *                                against. (required)
	 * @param marketplaceIds          The identifier for the marketplace in which
	 *                                you want to determine eligibility. Required
	 *                                only when program&#x3D;INBOUND. (optional)
	 * @param progressListener        Progress listener
	 * @param progressRequestListener Progress request listener
	 * @return Call to execute
	 * @throws ApiException If fail to serialize the request body object
	 */
	public com.squareup.okhttp.Call getItemEligibilityPreviewCall(String asin, String program,
			List<String> marketplaceIds, final ProgressResponseBody.ProgressListener progressListener,
			final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
		Object localVarPostBody = null;

		// create path and map variables
		String localVarPath = "/fba/inbound/v1/eligibility/itemPreview";

		List<Pair> localVarQueryParams = new ArrayList<Pair>();
		List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
		if (marketplaceIds != null)
			localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("csv", "marketplaceIds", marketplaceIds));
		if (asin != null)
			localVarQueryParams.addAll(apiClient.parameterToPair("asin", asin));
		if (program != null)
			localVarQueryParams.addAll(apiClient.parameterToPair("program", program));

		Map<String, String> localVarHeaderParams = new HashMap<String, String>();

		Map<String, Object> localVarFormParams = new HashMap<String, Object>();

		final String[] localVarAccepts = { "application/json" };
		final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
		if (localVarAccept != null)
			localVarHeaderParams.put("Accept", localVarAccept);

		final String[] localVarContentTypes = { "application/json" };
		final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
		localVarHeaderParams.put("Content-Type", localVarContentType);

		if (progressListener != null) {
			apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
				@Override
				public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
						throws IOException {
					com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
					return originalResponse.newBuilder()
							.body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
				}
			});
		}

		String[] localVarAuthNames = new String[] {};
		return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams,
				localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
	}

	private com.squareup.okhttp.Call getItemEligibilityPreviewValidateBeforeCall(String asin, String program,
			List<String> marketplaceIds, final ProgressResponseBody.ProgressListener progressListener,
			final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {

		// verify the required parameter 'asin' is set
		if (asin == null) {
			throw new ApiException(
					"Missing the required parameter 'asin' when calling getItemEligibilityPreview(Async)");
		}

		// verify the required parameter 'program' is set
		if (program == null) {
			throw new ApiException(
					"Missing the required parameter 'program' when calling getItemEligibilityPreview(Async)");
		}

		com.squareup.okhttp.Call call = getItemEligibilityPreviewCall(asin, program, marketplaceIds, progressListener,
				progressRequestListener);
		return call;

	}

	/**
	 * 
	 * This operation gets an eligibility preview for an item that you specify. You
	 * can specify the type of eligibility preview that you want (INBOUND or
	 * COMMINGLING). For INBOUND previews, you can specify the marketplace in which
	 * you want to determine the item&#39;s eligibility. **Usage Plan:** | Rate
	 * (requests per second) | Burst | | ---- | ---- | | 1 | 1 | For more
	 * information, see \&quot;Usage Plans and Rate Limits\&quot; in the Selling
	 * Partner API documentation.
	 * 
	 * @param asin           The ASIN of the item for which you want an eligibility
	 *                       preview. (required)
	 * @param program        The program that you want to check eligibility against.
	 *                       (required)
	 * @param marketplaceIds The identifier for the marketplace in which you want to
	 *                       determine eligibility. Required only when
	 *                       program&#x3D;INBOUND. (optional)
	 * @return GetItemEligibilityPreviewResponse
	 * @throws ApiException If fail to call the API, e.g. server error or cannot
	 *                      deserialize the response body
	 */
	public GetItemEligibilityPreviewResponse getItemEligibilityPreview(String asin, String program,
			List<String> marketplaceIds) throws ApiException {
		ApiResponse<GetItemEligibilityPreviewResponse> resp = getItemEligibilityPreviewWithHttpInfo(asin, program,
				marketplaceIds);
		return resp.getData();
	}

	/**
	 * 
	 * This operation gets an eligibility preview for an item that you specify. You
	 * can specify the type of eligibility preview that you want (INBOUND or
	 * COMMINGLING). For INBOUND previews, you can specify the marketplace in which
	 * you want to determine the item&#39;s eligibility. **Usage Plan:** | Rate
	 * (requests per second) | Burst | | ---- | ---- | | 1 | 1 | For more
	 * information, see \&quot;Usage Plans and Rate Limits\&quot; in the Selling
	 * Partner API documentation.
	 * 
	 * @param asin           The ASIN of the item for which you want an eligibility
	 *                       preview. (required)
	 * @param program        The program that you want to check eligibility against.
	 *                       (required)
	 * @param marketplaceIds The identifier for the marketplace in which you want to
	 *                       determine eligibility. Required only when
	 *                       program&#x3D;INBOUND. (optional)
	 * @return ApiResponse&lt;GetItemEligibilityPreviewResponse&gt;
	 * @throws ApiException If fail to call the API, e.g. server error or cannot
	 *                      deserialize the response body
	 */
	public ApiResponse<GetItemEligibilityPreviewResponse> getItemEligibilityPreviewWithHttpInfo(String asin,
			String program, List<String> marketplaceIds) throws ApiException {
		com.squareup.okhttp.Call call = getItemEligibilityPreviewValidateBeforeCall(asin, program, marketplaceIds, null,
				null);
		Type localVarReturnType = new TypeToken<GetItemEligibilityPreviewResponse>() {
		}.getType();
		return apiClient.execute(call, localVarReturnType);
	}

	/**
	 * (asynchronously) This operation gets an eligibility preview for an item that
	 * you specify. You can specify the type of eligibility preview that you want
	 * (INBOUND or COMMINGLING). For INBOUND previews, you can specify the
	 * marketplace in which you want to determine the item&#39;s eligibility.
	 * **Usage Plan:** | Rate (requests per second) | Burst | | ---- | ---- | | 1 |
	 * 1 | For more information, see \&quot;Usage Plans and Rate Limits\&quot; in
	 * the Selling Partner API documentation.
	 * 
	 * @param asin           The ASIN of the item for which you want an eligibility
	 *                       preview. (required)
	 * @param program        The program that you want to check eligibility against.
	 *                       (required)
	 * @param marketplaceIds The identifier for the marketplace in which you want to
	 *                       determine eligibility. Required only when
	 *                       program&#x3D;INBOUND. (optional)
	 * @param callback       The callback to be executed when the API call finishes
	 * @return The request call
	 * @throws ApiException If fail to process the API call, e.g. serializing the
	 *                      request body object
	 */
	public com.squareup.okhttp.Call getItemEligibilityPreviewAsync(String asin, String program,
			List<String> marketplaceIds, final ApiCallback<GetItemEligibilityPreviewResponse> callback)
			throws ApiException {

		ProgressResponseBody.ProgressListener progressListener = null;
		ProgressRequestBody.ProgressRequestListener progressRequestListener = null;

		if (callback != null) {
			progressListener = new ProgressResponseBody.ProgressListener() {
				@Override
				public void update(long bytesRead, long contentLength, boolean done) {
					callback.onDownloadProgress(bytesRead, contentLength, done);
				}
			};

			progressRequestListener = new ProgressRequestBody.ProgressRequestListener() {
				@Override
				public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {
					callback.onUploadProgress(bytesWritten, contentLength, done);
				}
			};
		}

		com.squareup.okhttp.Call call = getItemEligibilityPreviewValidateBeforeCall(asin, program, marketplaceIds,
				progressListener, progressRequestListener);
		Type localVarReturnType = new TypeToken<GetItemEligibilityPreviewResponse>() {
		}.getType();
		apiClient.executeAsync(call, localVarReturnType, callback);
		return call;
	}

	public static class Builder {
		private AWSAuthenticationCredentials awsAuthenticationCredentials;
		private LWAAuthorizationCredentials lwaAuthorizationCredentials;
		private String endpoint;
		private LWAAccessTokenCache lwaAccessTokenCache;
		private Boolean disableAccessTokenCache = false;
		private AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider;

		public Builder awsAuthenticationCredentials(AWSAuthenticationCredentials awsAuthenticationCredentials) {
			this.awsAuthenticationCredentials = awsAuthenticationCredentials;
			return this;
		}

		public Builder lwaAuthorizationCredentials(LWAAuthorizationCredentials lwaAuthorizationCredentials) {
			this.lwaAuthorizationCredentials = lwaAuthorizationCredentials;
			return this;
		}

		public Builder endpoint(String endpoint) {
			this.endpoint = endpoint;
			return this;
		}

		public Builder lwaAccessTokenCache(LWAAccessTokenCache lwaAccessTokenCache) {
			this.lwaAccessTokenCache = lwaAccessTokenCache;
			return this;
		}

		public Builder disableAccessTokenCache() {
			this.disableAccessTokenCache = true;
			return this;
		}

		public Builder awsAuthenticationCredentialsProvider(
				AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider) {
			this.awsAuthenticationCredentialsProvider = awsAuthenticationCredentialsProvider;
			return this;
		}

		public FbaInboundApi build() {
			if (awsAuthenticationCredentials == null) {
				throw new RuntimeException("AWSAuthenticationCredentials not set");
			}

			if (lwaAuthorizationCredentials == null) {
				throw new RuntimeException("LWAAuthorizationCredentials not set");
			}

			if (StringUtil.isEmpty(endpoint)) {
				throw new RuntimeException("Endpoint not set");
			}

			AWSSigV4Signer awsSigV4Signer;
			if (awsAuthenticationCredentialsProvider == null) {
				awsSigV4Signer = new AWSSigV4Signer(awsAuthenticationCredentials);
			} else {
				awsSigV4Signer = new AWSSigV4Signer(awsAuthenticationCredentials, awsAuthenticationCredentialsProvider);
			}

			LWAAuthorizationSigner lwaAuthorizationSigner = null;
			if (disableAccessTokenCache) {
				lwaAuthorizationSigner = new LWAAuthorizationSigner(lwaAuthorizationCredentials);
			} else {
				if (lwaAccessTokenCache == null) {
					lwaAccessTokenCache = new LWAAccessTokenCacheImpl();
				}
				lwaAuthorizationSigner = new LWAAuthorizationSigner(lwaAuthorizationCredentials, lwaAccessTokenCache);
			}

			return new FbaInboundApi(new ApiClient().setAWSSigV4Signer(awsSigV4Signer)
					.setLWAAuthorizationSigner(lwaAuthorizationSigner).setBasePath(endpoint));
		}
	}
}
